package com.parktech.sun.domain.repository.industryservice.hibernate;

import com.parktech.sun.domain.model.industryservice.IndustryService;
import com.parktech.sun.domain.model.industryservice.IndustryServiceInfoCategory;
import com.parktech.sun.domain.model.industryservice.IndustryServiceInformation;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.hibernate.BaseRepositoryImpl;
import com.parktech.sun.domain.repository.industryservice.IndustryServiceRepository;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-12
 * Time: 下午10:48
 */
@Repository
public class IndustryServiceRepositoryImpl extends BaseRepositoryImpl<IndustryService> implements IndustryServiceRepository {

    public IndustryServiceInformation getInformationById(Long industryServiceInformationId) {
        return (IndustryServiceInformation) getSession().get(IndustryServiceInformation.class, industryServiceInformationId);
    }

    public void clearThenSaveOrUpdateInformation(IndustryServiceInformation industryServiceInformation) {
        getSession().clear();
        getSession().saveOrUpdate(industryServiceInformation);
    }

    public void saveOrUpdateInformation(IndustryServiceInformation industryServiceInformation) {
        getSession().saveOrUpdate(industryServiceInformation);
    }

    public void clearThenSaveOrUpdateCategory(IndustryServiceInfoCategory industryServiceInfoCategory) {
        getSession().clear();
        getSession().saveOrUpdate(industryServiceInfoCategory);
    }

    public IndustryServiceInfoCategory getByCategoryId(Long aLong) {
        return (IndustryServiceInfoCategory) getSession().get(IndustryServiceInfoCategory.class, aLong);
    }

    public void saveOrUpdateCategory(IndustryServiceInfoCategory industryServiceInfoCategory) {
        getSession().saveOrUpdate(industryServiceInfoCategory);
    }

    public List<IndustryServiceInformation> listTop7PolicyInfo() {
        Query query = getSession().createQuery(
                "select info " +
                        "from IndustryServiceInformation as info, IndustryServiceInfoCategory as cat " +
                        "where info.category = cat and info.publishStatus = 1 and cat.name=? order by info.orderCode, info.publishDate desc");
        query.setString(0, "产业政策");
        query.setMaxResults(7);
        return query.list();
    }

    public List<IndustryServiceInformation> listTop6RequirementInfo() {
        Query query = getSession().createQuery(
                "select info " +
                        "from IndustryServiceInformation as info, IndustryServiceInfoCategory as cat " +
                        "where info.category = cat and info.publishStatus = 1 and cat.name='产业需求' order by info.orderCode, info.publishDate desc");
        query.setMaxResults(5);
        return query.list();
    }
}
